const router = require('express').Router();

const { $request } = require('../plugin/request'),
      { baseUrl } = require('../config/api.conf');
/*
* 个人中心 my
*
* */

/*
* 我的简历
* */
router.get('/resume', (req, res, next) => {

    const TOKEN = req.cookies['USER_TOKEN'];
    $request({
        method: 'get',
        baseUrl: baseUrl,
        url: '/job/userCenter/getResumeInfoOfJobSystem',
        headers: {
            "Authorization": TOKEN ? "bearer "+TOKEN : ''
        }
    }).finally(data => {
        data = JSON.parse(data);

        res.userID = '';
        res.resumeID = '';
        // 基本信息
        res.baseDetail = {};
        // 期望职位
        res.postList = [];
        // 语言信息
        res.languageDetail = {};
        // 工作经历
        res.workExperienceList = [];
        // 项目经历
        res.projectList = [];
        // 学历信息
        res.edutionList = [];
        // 技能证书：语言/其他证书
        res.l_skillList = [];
        res.o_skillList = [];
        // 自我评价
        res.selfLabel = {};

        res.resumeGmtUpdate = '';
        if(data.message === 'success'){

            let _data = data.data;
            let { userDetailInfo, posIntentionList, languageInfo, resumeGmtUpdate, selfEvaluation } = _data,
                { userExtension, workExperienceList, projectExperienceList, educationList } = userDetailInfo;

            userExtension === null && (userExtension = {});

            res.userID = userExtension['userId'];
            res.resumeID = _data['resumeId'];

            let nowYear = new Date().getFullYear(),
                nowMonth = new Date().getMonth() + 1;
            // 转换工作经验，大于等于6个月=1年
            let workYear = '', workMonth = '';
            if(userExtension['workYear']){
                workYear = userExtension['workYear'].split('-')[0];
                workMonth = userExtension['workYear'].split('-')[1];
                if(workYear && workMonth){
                    if(+workYear === nowYear){
                        workYear = (nowMonth-workMonth >= 6) ? '1年工作经验' : '1年以内工作经验'
                    }else if(+workYear < nowYear){
                        workYear = `${ (nowYear-workYear + (nowMonth-workMonth >= 6 ? 1 : 0)) }年工作经验`
                    }
                }
            }
            // 转换年龄
            let birthday = '', birthdayMonth = '';
            if(userExtension['birthday']){
                birthday = userExtension['birthday'].split('-')[0];
                birthdayMonth = userExtension['birthday'].split('-')[1];
                if(birthday){
                    if(+birthday === nowYear){
                        birthday = '1岁'
                    }else if(+birthday < nowYear){
                        birthday = `${ (nowYear-birthday + (nowMonth-birthdayMonth >= 6 ? 1 : 0)) }岁`
                    }
                }
            }

            res.baseDetail = {
               isCertificate: userExtension['certificatePassed'] || false,
               userHead: userDetailInfo['picturePath'],
               userName: userDetailInfo['userName'],
               status: _data['statusOfSeekingJob'],
               sex: userDetailInfo['sex'],
               birthday: userExtension['birthday'],
               _birthday: birthday,
               workYear: userExtension['workYear'],
               _workYear: workYear,
               nation: userExtension['nationality'],
               location: userExtension['province'] + '-' + userExtension['city'] + (userExtension['county'] ? ('-' + userExtension['county']) : ''),
               phone: _data['tel'],
               email: _data['email'],
               degree: userExtension['highestDegree']
            };

            res.languageDetail = languageInfo;
            res.workExperienceList = workExperienceList;
            res.projectList = projectExperienceList;
            res.edutionList = educationList;
            res.postList = posIntentionList;
            res.resumeGmtUpdate = resumeGmtUpdate;
            userDetailInfo['userSkillList'].forEach(skill => {
                skill.languageName
                    ? res.l_skillList.push(skill)
                    : res.o_skillList.push(skill)
            });
            res.selfLabel = selfEvaluation
        }
        res.render('my/resume.ejs', {
            title: '语聘 - 个人中心 - 我的简历',
            resData: res,
            layout: 'shared/layout'
        })
    })
});

/*
* 投递记录
* */
router.get('/record', (req, res, next) => {
    res.filterType = 1;
    res.render('my/record.ejs', {
        title: '语聘 - 个人中心 - 投递记录',
        resData: res,
        layout: 'shared/layout'
    })
});
router.get('/record/:id', (req, res, next) => {
    let { id } = req.params;
    res.filterType = id;
    res.render('my/record.ejs', {
        title: '语聘 - 个人中心 - 投递记录',
        resData: res,
        layout: 'shared/layout'
    })
});
/*
* 职位收藏
* */
router.get('/collect', (req, res, next) => {
    res.render('my/collect.ejs', {
        title: '语聘 - 个人中心 - 职位收藏',
        resData: res,
        layout: 'shared/layout'
    })
});

/*
* 账号设置
* */
router.get('/setting', (req, res, next) => {

    const TOKEN = req.cookies['USER_TOKEN'];
    $request({
        method: 'get',
        baseUrl: baseUrl,
        url: '/rm/userExtension/findResumeByUserId',
        headers: {
            "Authorization": TOKEN ? "bearer "+TOKEN : ''
        }
    }).finally(data => {
        data = JSON.parse(data);
        res.userDetail = {};
        if(data.message === 'success'){
            res.userDetail = data.data
        }
        res.render('my/setting.ejs', {
            title: '语聘 - 个人中心 - 账号设置',
            resData: res,
            layout: 'shared/layout'
        })
    })
});



module.exports = router;